package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.query.ProductQueryObejct;
import cn.wolfcode.crm.service.IProductService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @RequestMapping("")
    public String index(){
        return "product";
    }

    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Product product,String cosePrice){
        BigDecimal cp = new BigDecimal(cosePrice);
        product.setCosePrice(cp);
        JsonResult jsonResult = new JsonResult();
        try {
            productService.insert(product);
        } catch (Exception e) {
            jsonResult.setMsg("保存失败");
            e.printStackTrace();
        }
        return jsonResult;
    }
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        JsonResult jsonResult = new JsonResult();
        try {
            productService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            jsonResult.setMsg("删除失败");
            e.printStackTrace();
        }
        return jsonResult;
    }

    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Product product,String cosePrice){
        JsonResult jsonResult = new JsonResult();
        BigDecimal cp = new BigDecimal(cosePrice);
        product.setCosePrice(cp);
        try {
            productService.updateByPrimaryKey(product);
        } catch (Exception e) {
            jsonResult.setMsg("更新失败");
            e.printStackTrace();
        }
        return jsonResult;
    }
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(ProductQueryObejct qo){
        /*查询结果*/
        PageResult pageResult = productService.query(qo);
        return pageResult;
    }
    @RequestMapping("listAll")
    @ResponseBody
    public List<Product> listAll(){
        /*查询结果*/
        List<Product> products= productService.listAll();
        System.out.println(products);
        return products;
    }

    //导出会员信息
    //导出表格
    @RequestMapping("export")
    @ResponseBody
    public void export(ProductQueryObejct qo, HttpServletResponse response) throws Exception {

        //1.查询出列表数据
        qo.setRows(10000);
        List emps = productService.queryForList(qo);

        response.setHeader("Content-Disposition", "attachment;filename=product.xls");
        OutputStream outputStream = response.getOutputStream();

        //创建一个工作簿,目标是 outputStream
        WritableWorkbook workbook = Workbook.createWorkbook(outputStream);

        //2.处理标题
        //创建一个可以写 sheet ,
        WritableSheet sheet = workbook.createSheet("employee", 0);

        //创建一个单元格
        Label usernameLabel = new Label(0, 0, "产品名称");
        Label realNameLabel = new Label(1, 0, "产品编号");
        Label telLabel = new Label(2, 0, "单位");
        Label emailLabel = new Label(3, 0, "售价");
        Label deptLabel = new Label(4, 0, "数量");
        Label inputTimeLabel = new Label(5, 0, "入库时间");
        Label stateLabel = new Label(6, 0, "店铺");

        sheet.addCell(usernameLabel);
        sheet.addCell(realNameLabel);
        sheet.addCell(telLabel);
        sheet.addCell(emailLabel);
        sheet.addCell(deptLabel);
        sheet.addCell(inputTimeLabel);
        sheet.addCell(stateLabel);


        //3.循环迭代list,每一个employee 对应表格中的一个行数据,二每一个employe的每一个字段,应该对应一行的某一个单元格
        for(int i = 0; i < emps.size() ;i++){
            Product emp  = (Product) emps.get(i);

            int row = i+1;

            Label tempusernameLabel = new Label(0, row, emp.getProductName());
            Label temprealNameLabel = new Label(1, row, emp.getSn());
            Label temptelLabel = new Label(2, row, emp.getUnit());
            Label tempemailLabel = new Label(3, row, emp.getCosePrice().toString());
            Label tempdeptLabel = new Label(4, row, emp.getNumber().toString());
            Label tempinputTimeLabel = new Label(5, row, emp.getIncom().toLocaleString());
            Label tempShop = new Label(5, row, emp.getShop().getName());

            sheet.addCell(tempusernameLabel);
            sheet.addCell(temprealNameLabel);
            sheet.addCell(temptelLabel);
            sheet.addCell(tempemailLabel);
            sheet.addCell(tempdeptLabel);
            sheet.addCell(tempinputTimeLabel);
            sheet.addCell(tempShop);
        }
        //4.下文件
        //把内容中的工作簿对象的相关内容写到 输出流 里面
        workbook.write();
        //关闭工作簿
        workbook.close();
    }

}
